package leecode.面试题;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:
 * author: wangxi
 * date:  2022/4/18 5:47 下午
 **/
public class Question01 {
    public static void main(String[] args) {
        Question01 question01 = new Question01();
        String[] nums = {"aa", "aa", "cc","aa", "cc", "bb", "dd","dd","dd","dd","dd"};
        question01.fun01(nums);
    }

    public void fun01(String[] nums) {
        if (nums == null || nums.length <= 0) {
            return;
        }
        Map<String, Integer> dict = new HashMap<>();
        for (String s : nums) {
            dict.put(s, dict.getOrDefault(s, 0) + 1);
        }

        System.out.println(dict);

        List<Node> list = new ArrayList<>();
        dict.forEach((k, v) -> {
            Node node = new Node(v, k);
            list.add(node);
        });
        list.sort((x, y) -> Integer.compare(y.count, x.count));
        System.out.println(list);
    }

    public class Node {
        int count;
        String num;
        public Node(int count, String num) {
            this.count = count;
            this.num = num;
        }

        @Override
        public String toString() {
            return num;
        }
    }
}
